﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AleCrono
{
    /// <summary>
    /// Logica di interazione per setcountdown.xaml
    /// </summary>
    public partial class setcountdown : Window
    {
        public setcountdown()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            textBox1.Focus();
        }
        #region Funzioni
        private long ConvertSetTime(string theTime)
        {
            long seconds = 0;
            if (theTime.Trim().Substring(0, 1) == ":") // if leftmost colon, take it off
                theTime = theTime.Substring(1);
            string[] vals = theTime.Split(new char[] { ':', ' ', '.', '/' });
            for (int i = 0; i < vals.Length; i++)
            {
                seconds += Convert.ToInt32(vals[i]) * (long)Math.Pow((double)60, (double)(vals.Length - (1 + i)));
            }

            return seconds;
        }
        public string ConvertToTime(long tickCount)
        {
            // tickcount is in milliseconds, convert to minutes and seconds
            long seconds = tickCount;
            string val = (seconds / 60).ToString("00") + ":" + (seconds % 60).ToString("00");
            return val;
        }



        #endregion

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (textBox1.Text != String.Empty)
                {
                    AleCronoWPF.WinCronometro.StartTimeCD = ConvertSetTime(textBox1.Text);
                }
                else
                {
                    AleCronoWPF.WinCronometro.StartTimeCD = 0;
                }
                this.Close();
            }
            catch
            {
                MessageBox.Show("Valore Tempo non ammesso", "Errore CountDown");
                textBox1.Text = String.Empty;
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    if (textBox1.Text != String.Empty)
                    {
                        AleCronoWPF.WinCronometro.StartTimeCD = ConvertSetTime(textBox1.Text);
                    }
                    else
                    {
                        AleCronoWPF.WinCronometro.StartTimeCD = 0;
                    }
                    this.Close();
                }
            }
            catch
            {
                MessageBox.Show("Valore Tempo non ammesso", "Errore CountDown");
                textBox1.Text = String.Empty;
            }
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textBox1.Text != String.Empty)
            {
                button1.IsEnabled = true;
            }
            else
            {
                button1.IsEnabled = false;
            }
        }
    }
}
